﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Cloak.Mvp.Autofac.ViewParameters
{
	/// <summary>
	/// A check that one of many sub-checks indicates a parameter is a view
	/// </summary>
	public sealed class CompositeViewParameterSpecification : IViewParameterSpecification
	{
		private readonly IEnumerable<IViewParameterSpecification> _specifications;

		/// <summary>
		/// Initializes a specification with the specified sub-checks
		/// </summary>
		/// <param name="specifications">Sub-checks which are ORed together to identify view parameters</param>
		public CompositeViewParameterSpecification(IEnumerable<IViewParameterSpecification> specifications)
		{
			Contract.Requires(specifications != null);

			_specifications = specifications;
		}

		/// <summary>
		/// Initializes a specification with the specified sub-checks
		/// </summary>
		/// <param name="specifications">Sub-checks which are ORed together to identify view parameters</param>
		public CompositeViewParameterSpecification(params IViewParameterSpecification[] specifications)
			: this(specifications as IEnumerable<IViewParameterSpecification>)
		{}

		/// <summary>
		/// Determines if any sub-check indicates the parameter represents a view
		/// </summary>
		/// <param name="parameter">The parameter to check for representing a view</param>
		/// <returns>Whether any sub-check indicates the specified parameter represents a view</returns>
		public bool IsView(ParameterInfo parameter)
		{
			return _specifications.Any(specification => specification.IsView(parameter));
		}
	}
}